package com.stone.sys.action;

/**
 * <pre>
 * 用户信息维护
 * @author jz
 * </pre>
 */

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.UserService;
import com.stone.utils.MainUtils;
import com.stone.utils.Resources;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class UserAction120402 extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private UserService userService;
	
	private TUser user;
	
	private String password;
	
	private String id;
	
	private String organName;
	
	private String userId;
	
	private String flagSQ;
	
	// 父节点
	private String parentID;
	
	private String ouid;
	private String organcid;
	
	public TOrgan getOrgan() {
		return organ;
	}

	private TOrgan organ;
	
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		
		// 修改密码
		if (id == null) {
			
			return "PSW_SUCCESS";
			
			// 密码重置
		} else if ("a".equals(id)){
			
			return "PSW_RESET_SUCCESS";
		// 修改进入
		} else if (!"0".equals(id)) {
			
			this.user = (TUser)userService.findById(Long.parseLong(id), TUser.class.getName());

			user.setOrgan((TOrgan)userService.findById(user.getOrgan().getId(),TOrgan.class.getName()));

			// 新建进入
		} else {
			this.user = new TUser();
			this.user.setId(Long.parseLong("0"));
			this.user.setState("1");
			this.user.setIsadmin("0");
			
		}

		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		// 新建时
		if (user.getId() == 0) {

			TOrgan o  = (TOrgan) userService.findById(user.getOrgan().getId(), TOrgan.class.getName());
			user.setOrgan(o);
			userService.save(user);	
			if (user.getSortid() == null) {
				userService.updateByHQL("update TUser set sortid = " + user.getId() + " where id = " + user.getId(), null);
				user.setSortid(Integer.parseInt(user.getId().toString()));
			}
		} else {
			
			TUser u = (TUser) userService.findById(user.getId(), TUser.class.getName());
			
			TOrgan o  = (TOrgan) userService.findById(user.getOrgan().getId(), TOrgan.class.getName());
			
			// 将页面提交的实体复制至u
			MainUtils.copy(user, u);
			u.setOrgan(o);
			this.user = u;
			userService.update(u);
			
		}

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	
	
	/**
	 * 保存密码
	 * @return
	 * @throws AppException
	 */
	public String updatePassword() throws AppException {
			
		userService.updatePassword(this.password, ((TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());

		//UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
	//	userServiceDIMIS.updatePassword(this.password, ((TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());

		super.setInfo("密码修改成功");
		
		return "PSW_SUCCESS";
	}
	
	/**
	 * 重置密码
	 * @return
	 * @throws AppException
	 */
	public String passwordReset() throws AppException {
		
		String loginId = super.getRequest().getParameter("loginId");
		
		
		List<HashMap<String, Object>> list = userService.getListByDHql(" u as u", "loginId = ?", new Object[]{loginId}, "", "TUser u");
			
		if (list.size() == 0) {
			super.setInfo("没有此用户");
		} else {
			user = (TUser) ((HashMap<String, Object>)list.get(0)).get("u");

			userService.updateByHQL("update TUser set password = ? where loginId = ?", new Object[] {Resources.getString("password", "struts"),loginId});
			super.setInfo("重置成功");
		}
		
		return "PSW_RESET_SUCCESS";
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		userService.doDelAll(id);
		
		return "DEL-SUCCESS";
		
	}
	
	/**
	 * 假删除将状态设为不可用
	 * @return
	 * @throws AppException
	 */
	public String recycle() throws AppException {
		
		userService.updateByHQL("update TUser set state = '0' where id in (" + id + ")", null);
		
		return "DEL-SUCCESS";
		
	}
	
	/**
	 * 验证
	 * @return
	 * @throws AppException
	 * @throws IOException 
	 */
	public void check() throws Exception {
		
		String out = "{}";
		
		// 密码验证
		if (password != null) {
			
			// 修改密码时验证密码
			long count = userService.getTotalByDHql(
					"id = ? and password = ?" , 
					new Object[]{((TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId(),(this.password)},
					"TUser");
			
			
			if (count == 0) {
				out = "{m:'当前密码输入不正确！'}";
			}
		} else {
			
			// 登陆账号验证
			long count = userService.getTotalByDHql("loginId=?", new Object[]{user.getLoginId()}, "TUser");
			
			if (count > 0) {
				out = "{m:'登录账号已经存在！'}";
			}
		}
		
		super.getResponse().getWriter().print(out);
		
	}
	
	/**
	 * 获得当前用户的权限
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getRoleByUserId() {

		List<HashMap<String,Object>> list = userService.getListByDHql(
				"r.name as name, r.id as id, r.notes as notes,(select count(*) from TUserRole tur where tur.roleId=r.id and tur.userId='" 
				+ userId + "') as rcount", 
				"",//"r.id not in ( select ur.roleId from TUser u, TUserRole ur where u.id = ur.userId and u.loginId = '" + Resources.getString("superadminname", "struts") + "')",
				null,
				" order by r.id", 
				" TRole r");

		// 当前页列表
		setReqestAttribute("list", list);

		return "USER_ROLE_SUCCESS";
	}
	
	/**
	 * 给用户授权
	 * @return
	 * @throws AppException
	 */	
	public String saveUserRole() throws IOException  {
	
		// 保存 userId:用户ID  id：角色Id flagSQ：0取消授权 1授权
		this.userService.saveUserRole(userId,id,flagSQ);
		
		return "REDIRECT_USER_ROLE_SUCCESS";
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public TUser getUser() {
		return user;
	}
	public void setUser(TUser user) {
		this.user = user;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOrganName() {
		return organName;
	}

	public void setOrganName(String organName) {
		this.organName = organName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setFlagSQ(String flagSQ) {
		this.flagSQ = flagSQ;
	}

	public String getParentID() {
		return parentID;
	}

	public void setParentID(String parentID) {
		this.parentID = parentID;
	}

	public String getOuid() {
		return ouid;
	}

	public void setOuid(String ouid) {
		this.ouid = ouid;
	}

	public String getOrgancid() {
		return organcid;
	}

	public void setOrgancid(String organcid) {
		this.organcid = organcid;
	}

}
